package cc.comac.saic.hkhy.jsrj.gj.inttools.parser;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.comac.saic.hkhy.jsrj.gj.inttools.constant.FileType;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shisongyan
 * @date 2025/11/18
 */
@Service
@Slf4j
public class IntToolDataParser {
    private final ParserFactory parserFactory;

    @Autowired
    public IntToolDataParser(ParserFactory parserFactory) {
        this.parserFactory = parserFactory;
    }

    /**
     * 解析数据并提取信息
     */
    public ParseResult parseData(String data) {
        if (data == null || data.trim().isEmpty()) {
            throw new IllegalArgumentException("数据不能为空");
        }

        DataParser parser = parserFactory.getParser(data);
        log.info("使用解析器: {}", parser.getParserType());

        try {
            return parser.parse(data);
        } catch (ParseException e) {
            log.error("数据解析失败", e);
            ParseResult result = new ParseResult(parser.getParserType());
            result.setSuccess(false);
            result.setErrorMessage(e.getMessage());
            return result;
        }
    }

    /**
     * 解析数据并提取信息
     */
    public ParseResult parseData(String data, FileType fileType) {
        if (data == null || data.trim().isEmpty()) {
            throw new IllegalArgumentException("数据不能为空");
        }

        DataParser parser = parserFactory.getParser(fileType);
        log.info("使用解析器: {}", parser.getParserType());

        try {
            return parser.parse(data);
        } catch (ParseException e) {
            log.error("数据解析失败", e);
            ParseResult result = new ParseResult(parser.getParserType());
            result.setSuccess(false);
            result.setErrorMessage(e.getMessage());
            return result;
        }
    }

    /**
     * 批量解析数据
     */
    public List<ParseResult> parseBatch(List<String> dataList) {
        return dataList.stream()
            .map(this::parseData)
            .collect(Collectors.toList());
    }
}
